package com.e.library.http;


import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.util.SparseArray;

import java.io.File;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Cache;
import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.ResponseBody;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Converter;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

/**
 * Created by lxy on 2016/7/10.
 */
public class EClient {

    private EApi eApi;
    private Retrofit retrofit;
    public static String session;
    private static volatile EClient instance;
    public static SparseArray<String> status = new SparseArray<>(31);

    public static EClient with(Context context){
        if (instance == null){
            synchronized (EClient.class){
                if (instance == null){
                    instance = new EClient(context);
                }
            }
        }

        return instance;
    }

    private EClient(Context context){

        Context cxt = context.getApplicationContext();
        HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.i("Response",message);
            }
        });
        logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        File file = new File(cxt.getCacheDir(),"cache");
        Cache cache = new Cache(file,1024*1024*10);//10mb

        OkHttpClient client = new OkHttpClient.Builder()
                .readTimeout(15, TimeUnit.SECONDS)
                .connectTimeout(15,TimeUnit.SECONDS)
                .addInterceptor(new ParamsInterceptor())
                .addInterceptor(logInterceptor)
                .retryOnConnectionFailure(true)
                .cache(cache)
                .build();


        retrofit = new Retrofit.Builder()
                .baseUrl(EApi.BASE_URL)
                .addConverterFactory(new ResponseFactory())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io()))
                .validateEagerly(true)
                .client(client)
                .build();


    }

    public <T> T createService(Class<T> clz) {
        return retrofit.create(clz);
    }

    public EApi api(){
        if (eApi == null){
            eApi = createService(EApi.class);
        }
        return eApi;
    }

    public static class ResponseFactory extends Converter.Factory{
        @Override
        public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
            return new ReponseConverter();
        }
    }

    public static class ReponseConverter implements Converter<ResponseBody,EResponse>{
        @Override
        public EResponse convert(ResponseBody value) throws IOException {
            EResponse response = new EResponse(value.string());
            if (response.isExpire()){
                throw new SessionExpireException("请重新登录再试！");
            }
            String sid = response.getSession();
            if (!TextUtils.isEmpty(sid)){
                session = sid;//将session保存下来
            }
            return response;
        }
    }

    /**
     * 公共参数拦截器
     */
    private static class ParamsInterceptor implements Interceptor{

        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {

            Request original = chain.request();
            HttpUrl originalHttpUrl = original.url();
            HttpUrl.Builder newUrlBuilder = originalHttpUrl.newBuilder();
            Request.Builder newRequestBuilder = original.newBuilder();
            if(original.body() instanceof FormBody){
                //post
                FormBody.Builder newFormBody = new FormBody.Builder();
                FormBody oldFormBody = (FormBody) original.body();
                for (int i = 0;i<oldFormBody.size();i++){
                    newFormBody.addEncoded(oldFormBody.encodedName(i),oldFormBody.encodedValue(i));
                }
                newFormBody.addEncoded("is_app","1");
                if (!TextUtils.isEmpty(session)){
                    newFormBody.addEncoded("session_id",session);
                }
                newRequestBuilder.post(newFormBody.build());
            }else {
                //get
                newUrlBuilder.addQueryParameter("is_app","1");
                if (!TextUtils.isEmpty(session)){
                    newUrlBuilder.addQueryParameter("session_id",session);
                }
            }

            HttpUrl url = newUrlBuilder.build();
            return chain.proceed(newRequestBuilder.url(url).build());
        }

    }

//    public static void subscribe(Observable<EResponse> observable, final EResponseListener listener) {
//        EResponseSubscriber subscriber = new EResponseSubscriber(){
//            @Override
//            public void onStart() {
//            }
//
//            @Override
//            public void onCompleted() {
//            }
//
//            @Override
//            public void onFail(String msg) {
//                super.onFail(msg);
//            }
//        }.callback(listener);
//        observable.subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(subscriber);
//
//    }

    //状态码
    static {
        status.put(-1,"系统异常，请稍后再试");
        status.put(400001,"手机号或密码不能为空");
        status.put(400002,"用户不存在");
        status.put(400003,"账号或密码错误");
        status.put(400004,"注册失败");
        status.put(300001,"上传图片类型不正确");
        status.put(300002,"保存图片失败");
        status.put(300003,"上传图片校验失败");
        status.put(300004,"上传图片大小超过限制");
        status.put(400006,"邮箱验证码错误");
        status.put(400007,"短信验证码错误");
        status.put(400008,"上传图片不能为空");

        status.put(400009,"请退出重新登录再试");
        status.put(400010,"短信接口错误");
        status.put(400011,"没有好友");
        status.put(400012,"密码由数字和字母组成");
        status.put(400000,"请求出错了，请稍后再试");
        status.put(400013,"邮箱接口错误");
        status.put(400014,"用户id或者对象id为空");
        status.put(400015,"内容不全或数据丢失");
        status.put(400016,"邮箱验证码错误");
        status.put(400017,"需要操作的内容不为空");
        status.put(400018,"获取用户详细信息错误");

        status.put(100000,"该方法需要登录才能进行操作");
        status.put(100001,"非法访问");
        status.put(400019,"已加入收藏,请勿重复添加");
        status.put(400020,"下载失败，请确保您有足够的金币！");
        status.put(400021,"您今天的下载次数已用光！");
        status.put(400022,"不能关注自己哦，亲爱的！");
        status.put(400023,"获取用户详细信息错误");
        status.put(400024,"已关注该用户");
    }

}
